﻿Imports System.Data
Imports System.Data.SqlClient

Module moSMS

    Public Sub AutoSMS()
        Dim str As String = "SELECT ID,BirthDay FROM People WHERE Active = 1"
        Dim cmd As New SqlCommand(str, gCnnPCE)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet
        da.Fill(ds, "People")
        Dim iRowCount As Integer
        iRowCount = ds.Tables(0).Rows.Count
        Dim i As Integer
        For i = 0 To iRowCount - 1
            If CheckSendedThisYear(ds.Tables(0).Rows(i).Item("ID")) = False Then
                If CheckWhetherWantSend(ds.Tables(0).Rows(i).Item("ID")) Then
                    If ds.Tables(0).Rows(i).Item("Birthday") < Now Then
                        'MsgBox("SMS", MsgBoxStyle.Information)
                        InsertRecord2SMSRecord(ds.Tables(0).Rows(i).Item("ID"))
                    End If
                End If
            End If
        Next

    End Sub

    Private Function CheckSendedThisYear(ByVal PeopleID As Integer) As Boolean

        Dim str As String = "SELECT COUNT(*) FROM SMSRecords WHERE PeopleID = @PeopleID AND Year = " & Now.Year
        Dim cmd As New SqlCommand(str, gCnnPCE)
        cmd.Parameters.Add("@PeopleID", SqlDbType.Int).Value = PeopleID
        If cmd.ExecuteScalar > 0 Then
            Return True
        End If

        Return False

    End Function

    Private Function CheckWhetherWantSend(ByVal PeopleID As Integer) As Boolean

        Dim str As String = "SELECT BirthdaySMS FROM People WHERE ID = @PeopleID"
        Dim cmd As New SqlCommand(str, gCnnPCE)
        cmd.Parameters.Add("@PeopleID", SqlDbType.Int).Value = PeopleID
        If cmd.ExecuteScalar = 1 Then
            Return True
        End If

        Return False
    End Function

    Private Sub InsertRecord2SMSRecord(ByVal PeopleID As Integer)
        Dim str As String = "INSERT INTO SMSRecords (PeopleID, Year) VALUES (@PeopleID, @Year)"
        Dim cmd As New SqlCommand(str, gCnnPCE)
        cmd.Parameters.Add("@PeopleID", SqlDbType.Int).Value = PeopleID
        cmd.Parameters.Add("@Year", SqlDbType.Int).Value = Now.Year
        cmd.ExecuteNonQuery()

    End Sub


    Public Sub Func_SendSMS(ByVal MobileNo As String, ByVal Content As String, ByVal Unicode As Boolean)
        Dim strContent As String
        strContent = "un=" & OP_SMSID & "&" & _
     "pwd=" & OP_SMSPassword & "&" & _
     "dstno=" & (MobileNo) & "&" & _
     "msg=" & (Content) & "&" & _
     "type=" & IIf(Unicode = True, 2, 1) & "&" & _
     "sendid=" & OP_SMSSenderID


        '  strContent = "un=lkching7&pwd=leiling1983&dstno=60126387889&msg=%E6%B5%8B%E8%AF%95&type=2&sendid=lkching7"

        Dim web As New System.Net.WebClient()
        web.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
        Dim d As Byte()
        'Dim d As Byte() = System.Text.Encoding.ASCII.GetBytes("un=lkching7&pwd=leiling1983&dstno=60124255641&msg=Testing SMS!! Sending From Zhen-PRM Software&type=1&sendid=60126387889")
        If Unicode = True Then
            d = System.Text.Encoding.UTF8.GetBytes(strContent)
        Else
            d = System.Text.Encoding.ASCII.GetBytes(strContent)
        End If

        Dim res As Byte() = web.UploadData("http://isms.mobiweb.com.my/isms_send.php", "POST", d)
        Dim strRes As String = System.Text.Encoding.ASCII.GetString(res)
        'Select Case LanguageType
        '    Case "Default"
        '        MsgBox("Message Sended " & strRes, MsgBoxStyle.Information)
        '    Case "zh-CN"
        '        MsgBox("短信发送成功 " & strRes, MsgBoxStyle.Information)
        'End Select

    End Sub

End Module
